package com.jh.paymentchannel.util.uns;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Encrypt {
	
//	public static void main(String[] args) throws UnsupportedEncodingException {
//        String result = md5("accountId=1120140210111812001&name=张成&cardNo=623625856258000&orderId=201706200940&purpose=学费&amount=0.01&responseUrl=http:// IP:PORT&key=123456");
//        System.out.println("result: " + result);
//        System.out.println("length: " + result.length());
//    }
	/**
	 * MD5 加密
	 * @param text 待加密的字符串
	 * @return  加密结果
	 * @throws UnsupportedEncodingException
	 */
	public static String md5(String text) throws UnsupportedEncodingException {
		MessageDigest msgDigest = null;
		try {
			msgDigest = MessageDigest.getInstance("MD5");
			msgDigest.update(text.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException("System doesn't support MD5 algorithm.");
		} catch (UnsupportedEncodingException e) {
			throw new UnsupportedEncodingException("encode error");
		}
		
		byte[] bytes = msgDigest.digest();
		byte tb;
		char low;
		char high;
		char tmpChar;
		String md5Str = new String();

		for (int i = 0; i < bytes.length; i++) {
			tb = bytes[i];
			tmpChar = (char) ((tb >>> 4) & 0x000f);
			if (tmpChar >= 10) {
				high = (char) (('a' + tmpChar) - 10);
			} else {
				high = (char) ('0' + tmpChar);
			}

			md5Str += high;
			tmpChar = (char) (tb & 0x000f);

			if (tmpChar >= 10) {
				low = (char) (('a' + tmpChar) - 10);
			} else {
				low = (char) ('0' + tmpChar);
			}

			md5Str += low;
		}
		return md5Str;
	}
}
